using System;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    public class SelectSolutionControl : UserControlWin, IFormControl
    {
        private IBOComboBoxSelector _selector;
        public event EventHandler<BOEventArgs<DMSolution>> SolutionSelected;
        private IControlFactory controlFactory;


        public SelectSolutionControl()
        {
            controlFactory = GlobalUIRegistry.ControlFactory;
            IButtonGroupControl buttonGroupControl = controlFactory.CreateButtonGroupControl();
            buttonGroupControl.AddButton("Load", "Load", Load_ClickHandler);
            ILabel label = controlFactory.CreateLabel("Please select the solution");
            _selector = controlFactory.CreateComboBoxSelector();
            IPanel panel = controlFactory.CreatePanel();
            ColumnLayoutManager columnLayoutManager = new ColumnLayoutManager(panel, controlFactory);
            columnLayoutManager.ColumnCount = 1;
            columnLayoutManager.AddControl(label);
            columnLayoutManager.AddControl(_selector);
            columnLayoutManager.AddControl(buttonGroupControl);
            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(panel, BorderLayoutManager.Position.Centre);
            BusinessObjectCollection<DMSolution> solutions = new BusinessObjectCollection<DMSolution>();
            solutions.LoadAll("SolutionName");
            _selector.BusinessObjectCollection = solutions;
        }

        public IBOComboBoxSelector Selector
        {
            get { return _selector; }
        }

        /// <summary>
        ///             Sets the form to control
        /// </summary>
        /// <param name="form">The form to control</param>
        public void SetForm(IFormHabanero form)
        {

        }


        private void Load_ClickHandler(object sender, EventArgs e)
        {
            try
            {
                if (this.SolutionSelected != null)
                {
                    if (this.Selector.SelectedBusinessObject == null)
                    {
                        controlFactory.ShowMessageBox("Please select a solution!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    this.SolutionSelected(this, new BOEventArgs<DMSolution>((DMSolution)this.Selector.SelectedBusinessObject));
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

    }
}